package com.rbcs.base.project.webapp;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.hibernate.criterion.MatchMode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.rbcs.base.model.Project;
import com.rbcs.base.model.ProjectType;
import com.rbcs.base.project.service.ProjectManager;
import com.rbcs.base.project.service.ProjectTypeManager;
import com.systop.common.modules.security.user.UserUtil;
import com.systop.common.modules.security.user.model.User;
import com.systop.core.dao.support.Page;
import com.systop.core.util.PageUtil;
import com.systop.core.webapp.struts2.action.DefaultCrudAction;

/**
 * 项目管理Action
 * 
 * @author Nice
 */
@SuppressWarnings("serial")
@Controller
@Scope(BeanDefinition.SCOPE_PROTOTYPE)
public class ProjectAction extends DefaultCrudAction<Project, ProjectManager> {

	@Autowired
	private ProjectTypeManager projectTypeManager;

	// 项目类别
	private List<ProjectType> projectTypes;

	// 类别ID
	private Integer projectTypeId;

	/**
	 * 编辑新建项目
	 */
	public String edit() {
		projectTypes = projectTypeManager.getTypes();
		return INPUT;
	}

	/** 保存项目 */
	public String save() {
		if (getModel().getId() == null) {// 新建项目
			getModel().setCreateTime(new Date());
		}
		if (projectTypeId != null && projectTypeId != 0) {
			getModel().setProjectType(projectTypeManager.get(projectTypeId));
		}
		return super.save();
	}

	/**
	 * 项目列表，根据当前登录用户查询归属自己的项目
	 */
	public String index() {
		restorePageData(getSelfProject());
		return INDEX;
	}

	/**
	 * 项目选择，同样查询属于自己的项目
	 */
	public String selectProject() {
		restorePageData(getSelfProject());
		return "select";
	}
	
	/**
	 * 项目选择，查询其当前登录用户所在的项目
	 */
	public String selectGroupProject() {
		restorePageData(getGroupProject());
		return "select";
	}
	
	/**
	 * 项目选择，查询所有的项目
	 */
	public String selectAllProject() {
		restorePageData(getAllProject());
		return "select";
	}
	
	/**
	 * 获的项目组成员所属的项目
	 * @return
	 */
	private Page getGroupProject() {
		List<Object> args = new ArrayList<Object>();
		StringBuffer hql = new StringBuffer("from Project p where 1 = 1 ");
		hql.append("and ( p.managerIds like ? or p.memberIds like ? )");
		args.add(MatchMode.ANYWHERE.toMatchString(getLoginUser().getId().toString()));
		args.add(MatchMode.ANYWHERE.toMatchString(getLoginUser().getId().toString()));		
		if (StringUtils.isNotBlank(getModel().getName())) {
			hql.append(" and p.name like ?");
			args.add(MatchMode.ANYWHERE.toMatchString(getModel().getName()));
		}
		if (projectTypeId != null) {
			hql.append(" and p.projectType.id = ?");
			args.add(projectTypeId);
		}
		page = PageUtil.getPage(getPageNo(), getPageSize());
		return getManager().pageQuery(page, hql.toString(), args.toArray());
	}

	/**
	 * 获的所有的项目
	 * @return
	 */
	private Page getAllProject() {
		List<Object> args = new ArrayList<Object>();
		StringBuffer hql = new StringBuffer("from Project p where 1 = 1 ");
		if (StringUtils.isNotBlank(getModel().getName())) {
			hql.append(" and p.name like ?");
			args.add(MatchMode.ANYWHERE.toMatchString(getModel().getName()));
		}
		if (projectTypeId != null) {
			hql.append(" and p.projectType.id = ?");
			args.add(projectTypeId);
		}
		page = PageUtil.getPage(getPageNo(), getPageSize());
		return getManager().pageQuery(page, hql.toString(), args.toArray());
	}
	
	// 获得自己的项目
	private Page getSelfProject() {
		List<Object> args = new ArrayList<Object>();
		StringBuffer hql = new StringBuffer("from Project p where 1 = 1 ");
		if (StringUtils.isNotBlank(getModel().getName())) {
			hql.append(" and p.name like ?");
			args.add(MatchMode.ANYWHERE.toMatchString(getModel().getName()));
		}
		page = PageUtil.getPage(getPageNo(), getPageSize());
		return getManager().pageQuery(page, hql.toString(), args.toArray());
	}

	/** 获得当前登录用户 */
	private User getLoginUser() {
		User user = UserUtil.getLoginUser(getRequest());
		return getManager().getDao().get(User.class, user.getId());
	}

	public List<ProjectType> getProjectTypes() {
		return projectTypes;
	}

	public Integer getProjectTypeId() {
		return projectTypeId;
	}

	public void setProjectTypeId(Integer projectTypeId) {
		this.projectTypeId = projectTypeId;
	}
}
